<?php

class Safetypatrol extends CI_Controller {
               
	function __construct()
	{
 		parent::__construct();
		$this->load->library('form_validation');
		$this->load->database();
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->model('ktig');
		//$this->load->model('a');
	}
	function index()
	{
		$this->load->helper('url');
		$this->load->view('header');
		
		$this->ktig->is_logged_in();
		if($this->session->userdata('role')==1)
		{
			if(date('d')<11) { $this->load->view('v_safetypatrol'); }
		}
		else if ($this->session->userdata('role')==5)
		{
			$this->load->view('v_safetypatrol');
		}
		else { redirect('land'); }
	}
	function input_form()
	{			
		$this->form_validation->set_rules('lokasi', 'Lokasi', 'required|xss_clean|max_length[50]');			
		$this->form_validation->set_rules('bagian', 'Bagian', 'required|xss_clean|max_length[50]');			
		$this->form_validation->set_rules('departemen', 'Departemen', 'required|max_length[50]');
	
		if ($this->form_validation->run() == FALSE) // validation hasn't been passed
		{
			redirect('safetypatrol');
		}
		else // passed validation proceed to post success logic
		{
			$this->success();
		}
	}
	function success()
	{
		$this->load->helper('url');
		
		$tanggal_input = $this->input->post('tanggal');
		$tanggal = date("Y-m-d", strtotime($tanggal_input));
		
		$jam_input = $this->input->post('jam');
		$jam = date("H:i:s", strtotime($jam_input));
		
		if($this->input->post('ditempatkan_sesuai_tempatnya')=="ya") { $ditempatkan_sesuai_tempatnya=1; } else if($this->input->post('ditempatkan_sesuai_tempatnya')=="tidak") { $ditempatkan_sesuai_tempatnya=2; } else { $ditempatkan_sesuai_tempatnya=null; }
		if($this->input->post('dipilah_sesuai_sifatnya')=="ya") { $dipilah_sesuai_sifatnya=1; } else if($this->input->post('dipilah_sesuai_sifatnya')=="tidak") { $dipilah_sesuai_sifatnya=2; } else { $dipilah_sesuai_sifatnya=null; }
		if($this->input->post('kemasan')=="ya") { $kemasan=1; } else if($this->input->post('kemasan')=="tidak") { $kemasan=2; } else { $kemasan=null; }
		if($this->input->post('adalabel')=="ya") { $adalabel=1; } else if($this->input->post('adalabel')=="tidak") { $adalabel=2; } else { $adalabel=null; }
		if($this->input->post('tempat')=="ya") { $tempat=1; } else if($this->input->post('tempat')=="tidak") { $tempat=2; } else { $tempat=null; }
		if($this->input->post('msds')=="ya") { $msds=1; } else if($this->input->post('msds')=="tidak") { $msds=2; } else { $msds=null; }
		if($this->input->post('apd')=="ya") { $apd=1; } else if($this->input->post('apd')=="tidak") { $apd=2; } else { $apd=null; }
		if($this->input->post('lhyd')=="ya") { $lhyd=1; } else if($this->input->post('lhyd')=="tidak") { $lhyd=2; } else { $lhyd=null; }
		if($this->input->post('fhyd')=="ya") { $fhyd=1; } else if($this->input->post('fhyd')=="tidak") { $fhyd=2; } else { $fhyd=null; }
		if($this->input->post('alarm')=="ya") { $alarm=1; } else if($this->input->post('alarm')=="tidak") { $alarm=2; } else { $alarm=null; }
		if($this->input->post('air')=="ya") { $air=1; } else if($this->input->post('air')=="tidak") { $air=2; } else { $air=null; }
		
		if($this->input->post('canda')=="ya") { $canda=1; } else if($this->input->post('canda')=="tidak") { $canda=2; } else { $canda=null; }
		if($this->input->post('aman')=="ya") { $aman=1; } else if($this->input->post('aman')=="tidak") { $aman=2; } else { $aman=null; }
		if($this->input->post('apdd')=="ya") { $apdd=1; } else if($this->input->post('apdd')=="tidak") { $apdd=2; } else { $apdd=null; }
		if($this->input->post('sim')=="ya") { $sim=1; } else if($this->input->post('sim')=="tidak") { $sim=2; } else { $sim=null; }
		if($this->input->post('waktu')=="ya") { $waktu=1; } else if($this->input->post('waktu')=="tidak") { $waktu=2; } else { $waktu=null; }
		if($this->input->post('airm')=="ya") { $airm=1; } else if($this->input->post('airm')=="tidak") { $airm=2; } else { $airm=null; }
		
		if($this->input->post('sop')=="ya") { $sop=1; } else if($this->input->post('sop')=="tidak") { $sop=2; } else { $sop=null; }
		if($this->input->post('bhy')=="ya") { $bhy=1; } else if($this->input->post('bhy')=="tidak") { $bhy=2; } else { $bhy=null; }
		if($this->input->post('jsa')=="ya") { $jsa=1; } else if($this->input->post('jsa')=="tidak") { $jsa=2; } else { $jsa=null; }
		
		if($this->input->post('laik')=="ya") { $laik=1; } else if($this->input->post('laik')=="tidak") { $laik=2; } else { $laik=null; }
		if($this->input->post('prl')=="ya") { $prl=1; } else if($this->input->post('prl')=="tidak") { $prl=2; } else { $prl=null; }
		if($this->input->post('kal')=="ya") { $kal=1; } else if($this->input->post('kal')=="tidak") { $kal=2; } else { $kal=null; }
		if($this->input->post('tng')=="ya") { $tng=1; } else if($this->input->post('tng')=="tidak") { $tng=2; } else { $tng=null; }
		if($this->input->post('sak')=="ya") { $sak=1; } else if($this->input->post('sak')=="tidak") { $sak=2; } else { $sak=null; }
		
		if($this->input->post('lad')=="ya") { $lad=1; } else if($this->input->post('lad')=="tidak") { $lad=2; } else { $lad=null; }
		if($this->input->post('ven')=="ya") { $ven=1; } else if($this->input->post('ven')=="tidak") { $ven=2; } else { $ven=null; }
		if($this->input->post('pen')=="ya") { $pen=1; } else if($this->input->post('pen')=="tidak") { $pen=2; } else { $pen=null; }
		if($this->input->post('bis')=="ya") { $bis=1; } else if($this->input->post('bis')=="tidak") { $bis=2; } else { $bis=null; }
		if($this->input->post('pap')=="ya") { $pap=1; } else if($this->input->post('pap')=="tidak") { $pap=2; } else { $pap=null; }
		if($this->input->post('ding')=="ya") { $ding=1; } else if($this->input->post('ding')=="tidak") { $ding=2; } else { $ding=null; }
		if($this->input->post('sih')=="ya") { $sih=1; } else if($this->input->post('ding')=="tidak") { $sih=2; } else { $sih=null; }
		if($this->input->post('ramb')=="ya") { $ramb=1; } else if($this->input->post('ramb')=="tidak") { $ramb=2; } else { $ramb=null; }
		if($this->input->post('hala')=="ya") { $hala=1; } else if($this->input->post('hala')=="tidak") { $hala=2; } else { $hala=null; }
		if($this->input->post('rga')=="ya") { $rga=1; } else if($this->input->post('rga')=="tidak") { $rga=2; } else { $rga=null; }
		if($this->input->post('wc')=="ya") { $wc=1; } else if($this->input->post('wc')=="tidak") { $wc=2; } else { $wc=null; }
		if($this->input->post('dra')=="ya") { $dra=1; } else if($this->input->post('dra')=="tidak") { $dra=2; } else { $dra=null; }
		
		$data=array(
			'JENIS_FORM'=>1,
			'LOKASI'=>$this->input->post('lokasi'),
			'BAGIAN'=>$this->input->post('bagian'),
			'DEPARTEMEN'=>$this->input->post('departemen'),
			'TANGGAL'=>$tanggal,
			'JAM'=>$jam,
			
			'B1'=>$ditempatkan_sesuai_tempatnya,
			'KB1'=>$this->input->post('ket1'),
			'B2'=>$dipilah_sesuai_sifatnya,
			'KB2'=>$this->input->post('ket2'),
			'B3'=>$kemasan,
			'KB3'=>$this->input->post('ket3'),
			'B4'=>$adalabel,
			'KB4'=>$this->input->post('ket4'),
			'B5'=>$tempat,
			'KB5'=>$this->input->post('ket5'),
			'B6'=>$msds,
			'KB6'=>$this->input->post('ket6'),
			'B7'=>$apd,
			'KB7'=>$this->input->post('ket7'),
			'B8'=>$lhyd,
			'KB8'=>$this->input->post('ket8'),
			'B9'=>$fhyd,
			'KB9'=>$this->input->post('ket9'),
			'B10'=>$alarm,
			'KB10'=>$this->input->post('ket10'),
			'B11'=>$air,
			'KB11'=>$this->input->post('ket11'),
			
			'M1'=>$canda,
			'KM1'=>$this->input->post('ket12'),
			'M2'=>$aman,
			'KM2'=>$this->input->post('ket13'),
			'M3'=>$apdd,
			'KM3'=>$this->input->post('ket14'),
			'M4'=>$sim,
			'KM4'=>$this->input->post('ket15'),
			'M5'=>$waktu,
			'KM5'=>$this->input->post('ket16'),
			'M6'=>$airm,
			'KM6'=>$this->input->post('ket17'),
			
			'C1'=>$sop,
			'KC1'=>$this->input->post('ket18'),
			'C2'=>$bhy,
			'KC2'=>$this->input->post('ket19'),
			'C3'=>$jsa,
			'KC3'=>$this->input->post('ket20'),
			
			'P1'=>$laik,
			'KP1'=>$this->input->post('ket21'),
			'P2'=>$prl,
			'KP2'=>$this->input->post('ket22'),
			'P3'=>$kal,
			'KP3'=>$this->input->post('ket23'),
			'P4'=>$tng,
			'KP4'=>$this->input->post('ket24'),
			'P5'=>$sak,
			'KP5'=>$this->input->post('ket25'),
			
			'L1'=>$lad,
			'KL1'=>$this->input->post('ket26'),
			'L2'=>$ven,
			'KL2'=>$this->input->post('ket27'),
			'L3'=>$pen,
			'KL3'=>$this->input->post('ket28'),
			'L4'=>$bis,
			'KL4'=>$this->input->post('ket29'),
			'L5'=>$pap,
			'KL5'=>$this->input->post('ket30'),
			'L6'=>$ding,
			'KL6'=>$this->input->post('ket31'),
			'L7'=>$sih,
			'KL7'=>$this->input->post('ket32'),
			'L8'=>$ramb,
			'KL8'=>$this->input->post('ket33'),
			'L9'=>$hala,
			'KL9'=>$this->input->post('ket34'),
			'L10'=>$rga,
			'KL10'=>$this->input->post('ket35'),
			'L11'=>$wc,
			'KL11'=>$this->input->post('ket36'),
			'L12'=>$dra,
			'KL12'=>$this->input->post('ket37'),
			
			'ANGGOTA'=>$this->input->post('nama1'),
			'ANGGOTA2'=>$this->input->post('nama2'),
			'PENGAWAS'=>$this->input->post('pengawas'),
			'STATUS'=>1
		  );
		$this->db->insert('checklist_safety',$data);
		
		redirect('safetypatrol/lihat');
	}
	public function lihat()
	{
		$this->load->helper('url');
		$this->load->view('header');
		
		$data['datalist'] = $this->ktig->list_safetypatrol();
		
		$this->load->view('v_safetypatrol_lihat',$data);
	}
	public function hapus($id)
	{
		$this->load->helper('url');
		$this->ktig->hapus_safetypatrol($id);
		redirect('safetypatrol/lihat');
	}
	public function ubah($id)
	{
		$this->load->helper('url');
		$this->load->view('header');
		if($this->input->post('submit'))
		{
			$this->ktig->update_safetypatrol($id);
			redirect('safetypatrol/lihat');
		}
		$data['hasil']=$this->ktig->data_safetypatrol($id);
		$this->load->view('v_safetypatrol_ubah',$data);
	}
	public function detail($id)
	{
		$this->load->helper('url');
		$this->load->view('header');
		if($this->input->post('submit'))
		{
			redirect('safetypatrol/lihat');
		}
		
		$data['hasil']=$this->ktig->data_safetypatrol($id);
		$this->load->view('v_safetypatrol_detail',$data);
	}
}
?>